package com.clp.protocol.iec104.client.master_config;

import com.clp.protocol.iec104.client.pipeline.handler.MasterHandlersSupplier;
import com.clp.protocol.iec104.connect.config.ConnConfig;
import lombok.Getter;

import java.util.Collections;

/**
 * 主站连接配置
 */
@Getter
public class MasterConnConfig extends ConnConfig {
    /**
     * 默认最大重连次数
     */
    public static final int DEFAULT_MAX_RECONNECT_COUNT = 10;
    /**
     * 默认自动进行启动传输激活
     */
    public static final boolean DEFAULT_AUTO_START_DT_V = true;
    /**
     * 默认总召唤周期（秒）
     */
    public static final int DEFAULT_TOTAL_CALL_100_PERIOD_SECONDS = 10 * 60;
    /**
     * 默认电度量召唤周期（秒）
     */
    public static final int DEFAULT_TOTAL_CALL_101_PERIOD_SECONDS = 10 * 60;
    private static final MasterHandlersSupplier DEFAULT_HANDLER_SUPPLIER = master -> Collections.emptyList();

    /******************************************************************************************************************/

    private final int maxReconnectCount;

    /**
     * 初始化器
     */
    private final MasterHandlersSupplier supplier;
    /**
     * 是否自动进行启动传输激活
     */
    private final boolean autoStartDtV;

    /**
     * 总召唤周期
     * @param configurer
     */
    private final int totalCall100PeriodSeconds;

    private final int totalCall101PeriodSeconds;

    private MasterConnConfig(Configurer configurer) {
        super(configurer);
        this.maxReconnectCount = configurer.maxReconnectCount;
        this.supplier = configurer.supplier;
        this.autoStartDtV = configurer.autoStartDtV;
        this.totalCall100PeriodSeconds = configurer.totalCall100PeriodSeconds;
        this.totalCall101PeriodSeconds = configurer.totalCall101PeriodSeconds;
    }

    @Override
    public void check() throws Throwable {
        super.check();
    }

    public static Configurer configurer() {
        return new Configurer();
    }

    public static class Configurer extends ConnConfig.Configurer<Configurer> {
        private int maxReconnectCount = DEFAULT_MAX_RECONNECT_COUNT;
        private MasterHandlersSupplier supplier = DEFAULT_HANDLER_SUPPLIER;
        private boolean autoStartDtV = DEFAULT_AUTO_START_DT_V;
        private int totalCall100PeriodSeconds = DEFAULT_TOTAL_CALL_100_PERIOD_SECONDS;
        private int totalCall101PeriodSeconds = DEFAULT_TOTAL_CALL_101_PERIOD_SECONDS;

        public Configurer maxReconnectCount(int maxReconnectCount) {
            this.maxReconnectCount = maxReconnectCount;
            return this;
        }

        public Configurer customHandlersSupplier(MasterHandlersSupplier supplier) {
            this.supplier = supplier;
            return this;
        }

        public Configurer autoStartDtV(boolean autoStartDtV) {
            this.autoStartDtV = autoStartDtV;
            return this;
        }

        public Configurer totalCall100PeriodSeconds(int totalCallPeriodSeconds) {
            this.totalCall100PeriodSeconds = totalCallPeriodSeconds;
            return this;
        }

        public Configurer totalCall101PeriodSeconds(int totalCall101PeriodSeconds) {
            this.totalCall101PeriodSeconds = totalCall101PeriodSeconds;
            return this;
        }

        public MasterConnConfig configOk() {
            return new MasterConnConfig(this);
        }
    }
}
